<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;

class User extends Controller
{
    public function index()
    {
        // 原生SQL
//        $sql = "SELECT * FROM user";
//        $list = Db::query($sql);
//        $sql = "SELECT * FROM user WHERE id=?";
//        $list = Db::query($sql, [5]);
        // DB类
//        $list = Db::table('user')->order(['id'=>'desc'])->select();
        // 助手函数
        $list = db('user')->select();
        dump($list);
    }

    public function insert()
    {
        // 原生SQL
//        $sql = "INSERT INTO user (id,name,sex) VALUES (null,'小哈哈','0')";
//        $sql = "INSERT INTO user (name,sex) VALUES (:n,:s)";
//        $result = Db::execute($sql, ['n'=>'大哈哈', 's'=>'2']);
        // DB类
        $data = ['name'=>'哦哈哈','age'=>'100'];
//        $result = Db::table('user')->insert($data);
//        $result = Db::table('user')->data($data)->insert();
        // 助手函数
//        $result = db('user')->insert($data);
        $result = Db::table('user')->insertGetId($data);

        var_dump($result);
    }

    public function update()
    {
        // DB类
//        $result = Db::table('user')->where('id', 1)->update(['age'=>'19']);

        $result = db('user')->where('id', 1)->update(['age'=>'18']);

        var_dump($result);
    }

    public function delete()
    {
        // DB类
//        $result = Db::table('user')->delete(55);
        $result = db('user')->where(['name'=>'老王'])->delete();

        var_dump($result);
    }

}
